﻿using System;
using System.Web.UI;


namespace CodeAffinity.FormAbstraction
{
    public abstract class DataObjectPage : Page, IVisitedElement
    {
        /// <summary>
        /// represents current data objects as they were loaded. 
        /// </summary>
        public LoadMementos DataAsLoaded { get; set; }

        /// <summary>
        /// set to true to disable loading. False by default
        /// </summary>
        public bool DisableLoad { get; set; }

        public DataObjectPage()
        {
            DisableLoad = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
                Accept(new DataObjectLoader());
            else
                Accept(new DataObjectSaver());
        }

        #region IVisitedElement Members

        public void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }

        #endregion

        public Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
                return Root;

            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }

            return null;
        }
    }
}
